using System;
using System.Collections.Generic;
using System.Text;
using CodeStory.Core.UI;
using System.Web.UI;
using CodeStory.Gallery.ObjectBusiness;
using CodeStory.Core;
using CodeStory.Gallery.ObjectEntity;

namespace CodeStory.Gallery.Portlets
{
	[NavigationPortlet("CodeStory_Gallery_CreateGallery")]
	public class CreateGallery : Portlet
	{
		protected global::System.Web.UI.WebControls.TextBox txtGalleryName;
		//global::System.Web.UI.WebControls.TextBox txtDescription;
		protected global::System.Web.UI.WebControls.Button btnCreate;

		ITextControl txtDescription;

		protected override void OnInit(EventArgs e) {
			Load += new EventHandler(CreateGallery_Load);
			btnCreate.Click += new EventHandler(ButtonCreate_Click);
			txtDescription = FindControl("txtDescription") as ITextControl;
			base.OnInit(e);
		}

		void ButtonCreate_Click(object sender, EventArgs e) {
			//throw new Exception("The method or operation is not implemented.");
			string galleryid = Request.QueryString["galleryid"];
			string galleryname = txtGalleryName.Text;
			string description = txtDescription.Text;
			Guid userid = UserContext.Current.UserID;
			GalleryMainBusiness.InsertOrUpdate(galleryid,userid,galleryname,description);
		}

		void CreateGallery_Load(object sender, EventArgs e) {
			if (!GallerySetting.Current.AllowCreateGallery && !EditMode) {
				Visible = false;
				return;
			}
			if (GalleryHasCreated && !EditMode) {
				string galleryid = Request.QueryString["galleryid"];
				if (string.IsNullOrEmpty(galleryid)) {
					Visible = false;
					return;
				}
			}
			if (!IsPostBack) {
				BindGalleryMain();
			}
		}

		private bool GalleryHasCreated {
			get { return GalleryMainBusiness.GalleryHasCreated(UserContext.Current.UserID); }
		}

		private void BindGalleryMain() {
			string galleryid = Request.QueryString["galleryid"];
			GalleryMainObject obj;
			obj = GalleryMainBusiness.Read(galleryid);
			if (obj == null) return;
			txtGalleryName.Text = obj.GalleryName;
			txtDescription.Text = obj.Description;
		}
	}
}
